from django.shortcuts import render
import math
from datetime import datetime
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializer import *
# Create your views here.

class RegisterView(APIView):
    def post(self,reuqest):
        username = reuqest.data.get('username')
        password = reuqest.data.get('password')

        try:
            user = User.objects.get(username=username)
            return Response({'msg':'用户名存在','code':500})
        except Exception as e:
            user = User.objects.create(username=username,password=password)
            return Response({'msg':'注册成功','code':200,'uid':user.id})

class ZizhuView(APIView):
    def get(self,request):
        uid = request.query_params.get('uid')
        zizhu_obj = ZiZhu.objects.all()
        for i in zizhu_obj:
            if i.status==1:
                meishi_obj = Meishi.objects.filter(ZiZhu=i,user_id=uid,end_time__isnull=True).first()
                if meishi_obj:
                    i.is_user = 1
                else:
                    i.is_user = 0
            ser_obj = ZizhuSerializer(zizhu_obj,many=True)
            return Response({'msg':'OK','code':200,'data':ser_obj.data})

class MeishiView(APIView):
    def post(self,request):
        uid = request.data.get('uid')
        zid = request.data.get('zid')
        meishi_obj = Meishi.objects.create(user_id=uid,zizhu_id=zid,start_time=datetime.now())
        ZiZhu.objects.filter(pk=zid).update(status=1)
        return Response({'msg':'ok','code':200})

    def put(self,request):
        #结束食用
        uid = request.data.get('uid')
        zid = request.data.get('zid')
        # 获得该美食
        zizhu_obj = ZiZhu.objects.get(pk=zid)
        try:
            meishi_obj = Meishi.objects.get(user_id=uid,zizhu_id=zid,end_time__isnull=True)
            meishi_obj.end_time = datetime.now()
            # 食用时长
            hours = math.ceil((meishi_obj.end_time-meishi_obj.start_time).secounds/3600)
            count = Meishi.objects.filter(user_id=uid).count()
            if count <=8:
                meishi_obj.total_money = zizhu_obj.price*hours
            else:
                meishi_obj.total_money = zizhu_obj.price*hours*5
                # 结束时更改状态
                zizhu_obj.status=0
                zizhu_obj.save()
        except:
            # 如果开吃订单结束,获取该订单信息
            meishi_obj=Meishi.objects.filter(user_id=uid,zizhu_id=zid).order_by('-id').first()
            ser_obj = MeishiSerializer(meishi_obj)
            return Response({'msg':'OK','code':200,'data':ser_obj.data})

    def get(self,request):
        uid = request.data.get('uid')
        zid = request.data.get('zid')
        if uid:
            #获取用户食用记录
            meishi_obj = Meishi.objects.filter(user_id=uid)
        else:
            meishi_obj = Meishi.objects.filter(zizhu_id=zid)

            for i in meishi_obj:
                hours = math.ceil((i.end_time-i.start_time).seconds/3600)
                i.hours = hours
                ser_obj=MeishiSerializer(meishi_obj,many=True)
                return Response({'msg':'OK','code':200,'data':ser_obj.data})







